package com.dsfa.util;

import cn.hutool.core.date.DateTime;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 */
public class TimeUtil {
    public static Timestamp currentTime(){
        return new Timestamp(System.currentTimeMillis());
    }

    public final static String PatternShort = "yyyy-MM-dd";
    public final static String PatternLong = "yyyy-MM-dd HH:mm:ss";
    public final static String PatternJoin = "yyMMddHHmmss";//240115224644

    /**
     * 比较两个时间戳
     * @param timestamp1
     * @param timestamp2
     * @return  第一个比第二个多多少毫秒
     */
    public static long timeInterval(Timestamp timestamp1 ,Timestamp timestamp2){
        if(timestamp1 == null || timestamp2 == null)
            throw new RuntimeException("参数为空");
        return timestamp1.getTime() - timestamp2.getTime();
    }

    public static Date parseDate(String date,String pattern){
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getYear(Date d){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(Calendar.YEAR);
    }

    public static int getMonth(Date d){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 时间戳转换为时间字符串
     *
     * @param timeStamp 13位或10位时间戳，10位会自动*1000
     * @param pattern   "yyyy-MM-dd" || "yyyy-MM-dd HH:mm:ss"
     */
    public static String convertTime(long timeStamp, String pattern) {
        if (timeStamp < 10000000000L)
            timeStamp *= 1000;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date(timeStamp));
    }

    /**
     * @Description: 获取时间字符串 yyyy-MM-dd，默认获取当前
     * @ClassName: TimeUtil
     * @Author: wangdm
     * @Date: 2023/12/15
     */
    public static String getDateShortStr(Date d) {
        if (d == null) {
            d = Calendar.getInstance().getTime();
        }
        return new SimpleDateFormat(PatternShort).format(d);
    }

    /**
     * @Description: 获取时间字符串 yyyy-MM-dd HH:mm:ss，默认获取当前
     * @ClassName: TimeUtil
     * @Author: wangdm
     * @Date: 2023/12/15
     */
    public static String getDateLongStr(Date d) {
        if (d == null) {
            d = Calendar.getInstance().getTime();
        }
        return new SimpleDateFormat(PatternLong).format(d);
    }

    /**
     * @Description: 将日期字符串+1天，并返回新日期的yyyy-MM-dd格式化
     * @ClassName: TimeUtil
     * @Author: wangdm
     * @Date: 2023/12/15
     */
    public static String dateAddOne(String dd) {
        try {
            Date date = new SimpleDateFormat(PatternShort).parse(dd);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE, 1);
            date = calendar.getTime();
            return new SimpleDateFormat(PatternShort).format(date);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return "";
        }
    }
}
